package com.emcom.gotour.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.util.HtmlUtils;

import com.emcom.gotour.dao.TourCategoryDao;
import com.emcom.gotour.po.TourCategory;
import com.emcom.gotour.vo.CategoryVO;

public class TourCategoryServiceImpl implements TourCategoryService {

	private TourCategoryDao tourCategoryDao;

	public void delete(long id) {
		TourCategory parameters = new TourCategory();
		parameters.setId(id);
		tourCategoryDao.delete(parameters);
	}

	public void insert(String tourCategory, String typeId) {
		TourCategory parameters = new TourCategory();
		parameters.setName(HtmlUtils.htmlEscape(tourCategory));
		parameters.setTypeId(typeId);
		tourCategoryDao.insert(parameters);
	}

	public List<TourCategory> query(String typeId) {
		TourCategory parameters = new TourCategory();
		if(StringUtils.isNotEmpty(typeId)) {
			parameters.setTypeId(typeId);
		}
		return tourCategoryDao.query(parameters);
	}

	public List<CategoryVO> queryCategoryVO(String typeId, String name) {
		TourCategory parameters = new TourCategory();
		parameters.setTypeId(typeId);
		List<CategoryVO> categoryList = new ArrayList<CategoryVO>();
		List<TourCategory> tourCategoryList = tourCategoryDao.query(parameters);
		for(TourCategory tourCategory : tourCategoryList) {
			CategoryVO category = new CategoryVO();
			category.setId(tourCategory.getId());
			category.setName(tourCategory.getName());
			category.setTypeId(tourCategory.getTypeId());
			category.setSelected(false);
			if(StringUtils.equals(name, tourCategory.getName())) {
				category.setSelected(true);
			}
			categoryList.add(category);
		}
		return categoryList;
	}

	public List<String> queryCategory(String typeId) {
		List<TourCategory> tourCategorieList = query(typeId);
		List<String> categoryList = new ArrayList<String>();
		for(TourCategory tourCategory : tourCategorieList) {
			categoryList.add(tourCategory.getName());
		}
		return categoryList;
	}

	public void setTourCategoryDao(TourCategoryDao tourCategoryDao) {
		this.tourCategoryDao = tourCategoryDao;
	}

}
